﻿using AnimeLibrary.App_Code;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace AnimeLibrary
{
    public partial class list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtSearch_TextChanged(dgvAnimeLibraryList, null);
            }
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            DisableSwapDirection = true;

            if (txtSearch.Text.Length > 0)
                BindData(string.Format("SELECT * FROM tbl_anime_library WHERE ([Title] LIKE '%{0}%') ORDER BY [Title] ASC", txtSearch.Text));
            else
                BindData("SELECT * FROM tbl_anime_library ORDER BY [Title] ASC");
        }

        protected void dgvAnimeLibraryList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dgvAnimeLibraryList.PageIndex = e.NewPageIndex;

            DisableSwapDirection = true;
            dgvAnimeLibraryList.Sort(CurrentSortExpression, CurrentSortDirection);
        }

        protected void dgvAnimeLibraryList_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (GridDataSource != null)
            {
                CurrentSortExpression = e.SortExpression;
                e.SortDirection = ((DisableSwapDirection) ? CurrentSortDirection : SwapSortDirection());

                GridDataSource.DefaultView.Sort = string.Format("{0} {1}", e.SortExpression, ((e.SortDirection == SortDirection.Ascending) ? "ASC" : "DESC"));

                dgvAnimeLibraryList.DataSource = GridDataSource.DefaultView;
                dgvAnimeLibraryList.DataBind();

                DisableSwapDirection = false;
            }
        }

        private void BindData(string commandText)
        {
            try
            {
                DataTable dtAnimeLibrary = ReadOp.GetDataTable(Request.MapPath("App_Data\\AnimeLibrary.accdb"), commandText);

                GridDataSource = dtAnimeLibrary;

                dgvAnimeLibraryList.DataSource = GridDataSource;
                dgvAnimeLibraryList.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        private SortDirection SwapSortDirection()
        {
            CurrentSortDirection = ((CurrentSortDirection == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending);

            return CurrentSortDirection;
        }

        private SortDirection CurrentSortDirection
        {
            get { return ((ViewState["SortDirection"] == null) ? SortDirection.Ascending : (SortDirection)ViewState["SortDirection"]); }
            set { ViewState["SortDirection"] = value; }
        }

        private bool DisableSwapDirection
        {
            get { return ((ViewState["DisableSwapDirection"] == null) ? false : (bool)ViewState["DisableSwapDirection"]); }
            set { ViewState["DisableSwapDirection"] = value; }
        }

        private string CurrentSortExpression
        {
            get { return ((ViewState["SortExpression"] == null) ? "Title" : ViewState["SortExpression"].ToString()); }
            set { ViewState["SortExpression"] = value; }
        }

        private DataTable GridDataSource
        {
            get { return ((ViewState["GridDataSource"] == null) ? null : (DataTable)ViewState["GridDataSource"]); }
            set { ViewState["GridDataSource"] = value; }
        }
    }
}